package com.retoc.oawx;

import cn.hutool.core.util.StrUtil;
import com.retoc.oawx.common.SystemConstants;
import com.retoc.oawx.domain.SysConfig;
import com.retoc.oawx.mapper.SysConfigMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;

import javax.annotation.PostConstruct;
import java.io.File;
import java.lang.reflect.Field;
import java.util.List;

@SpringBootApplication
@ServletComponentScan
@Slf4j
@EnableAsync
public class OaWxApplication {

	@Autowired
	private SysConfigMapper sysConfigMapper;

	@Autowired
	private SystemConstants constants;

	@Value("${oawx.image-folder}")
	private String imageFolder;

	public static void main(String[] args) {
		SpringApplication.run(OaWxApplication.class, args);
	}

	@PostConstruct
	public void init(){
		List<SysConfig> list=sysConfigMapper.selectAllParam();
		list.forEach(one->{
			String key= StrUtil.toCamelCase(one.getParamKey());
			String value= one.getParamValue();
			try {
				Field field=constants.getClass().getDeclaredField(key);
				field.set(constants,value);
			} catch (Exception e) {
				log.error("执行异常",e);
			}
		});
		new File(imageFolder).mkdirs();
	}
}
